<?php

namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;
use Encore\Admin\Facades\Admin;
use App\Models\Details;
use App\Models\Semester;

class InsertTCQE
{
    protected $id;

    public function __construct($id, $appraiser_type)
    {
        $this->id = $id;
        $this->appraiser_type = $appraiser_type;
    }

    protected function script()
    {
        return <<<SCRIPT

$('.grid-check-row').on('click', function () {
    var id =  $(this).data('id');
    var appraiser_type = $(this).data('appraiser_type');

    location.href = "/admin/cqe/detail?id=" + id + "&type=" + appraiser_type;
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        $user_id = Admin::user()->id;
        $type = $this->appraiser_type;

        $semester = Semester::where('is_semester',1)->first();
        $semester_id = $semester->id;

        $judge = Details::where('student_id',$this->id)->where('appraiser',$user_id)->where('appraiser_type', $type)->where('semester_id',$semester_id)->first();

        if(empty($judge)) {
            $a = "<a class='btn btn-xs fa fa-pencil-square-o grid-check-row' data-id='{$this->id}' data-appraiser_type='{$this->appraiser_type}'>&nbsp;&nbsp;进行评价</a>";
        }else {
            $a = "<a href=\"javascript:;\" class='btn btn-xs fa fa-check' data-id='{$this->id}' data-appraiser_type='{$this->appraiser_type}'>&nbsp;&nbsp;已评价</a>";
        }

        return $a;
    }

    public function __toString()
    {
        return $this->render();
    }
}